package com.ruoyi.web.controller.his;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.his.domain.vo.HisDisposalStatistcs;
import com.ruoyi.his.domain.vo.HisWarningStatistcs;
import com.ruoyi.his.domain.vo.HisWarningStatistcsPre;
import com.ruoyi.his.service.IWarnHospitalLogService;
import com.ruoyi.his.service.IWarnPreLogService;
import com.ruoyi.system.domain.HisEngineRuleCommon;
import com.ruoyi.system.domain.HisLicensedOverWarning;
import com.ruoyi.system.domain.HisVerifyOverdueWarning;
import com.ruoyi.system.service.IHisEngineAnalysisService;
import com.ruoyi.system.service.IHisEngineRuleCommonService;
import com.ruoyi.system.service.IHisHospitalNationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * 通用规则逻辑Controller
 * 
 * @author Alan
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/his/statistics")
public class HisStatisticsController extends BaseController
{
    @Autowired
    private IHisHospitalNationService hisHospitalNationService;
    @Autowired
    private IHisEngineAnalysisService hisEngineAnalysisService;

    @Autowired
    private IWarnHospitalLogService warnHospitalLogService;
    @Autowired
    private IWarnPreLogService warnPreLogService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 首页数据统计
     */
    @GetMapping(value = "/list")
    public AjaxResult getlist()
    {
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }

        return AjaxResult.success(hisHospitalNationService.getlist(hospitalIds,signatures));
    }



    @ApiOperation("预警统计")
    @GetMapping("/warningstatistics")
    public TableDataInfo list(HisVerifyOverdueWarning hisVerifyOverdueWarning)
    {
        startPage();
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        hisVerifyOverdueWarning.setHospitalIds(hospitalIds);
        hisVerifyOverdueWarning.setSignatures(signatures);

        HisWarningStatistcsPre hisVerifyOverdueWarnings
                = hisEngineAnalysisService.getWarningStatisticsHospital(hisVerifyOverdueWarning);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg(String.valueOf(hisVerifyOverdueWarnings.getTotal()));
        rspData.setRows(hisVerifyOverdueWarnings.getList());
        rspData.setTotal(new PageInfo(hisVerifyOverdueWarnings.getList()).getTotal());
        return rspData;
    }

    @ApiOperation("处置统计")
    @GetMapping("/disposalstatistics")
    public TableDataInfo licencedWarning(HisLicensedOverWarning hisLicensedOverWarning)
    {
        startPage();
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        hisLicensedOverWarning.setHospitalIds(hospitalIds);
        hisLicensedOverWarning.setSignatures(signatures);
        HisWarningStatistcsPre hisVerifyOverdueWarnings
                = hisEngineAnalysisService.getWarningStatistics(hisLicensedOverWarning);
        TableDataInfo rspData = new TableDataInfo();
        List<HisWarningStatistcs> list = hisVerifyOverdueWarnings.getList();
        int sum = list.stream().mapToInt(HisWarningStatistcs::getTotal).sum();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg(String.valueOf(sum));
        rspData.setRows(hisVerifyOverdueWarnings.getList());
        rspData.setTotal(new PageInfo(hisVerifyOverdueWarnings.getList()).getTotal());
        return rspData;
    }
    @GetMapping(value = "/disposalstatisticsInfo/{id}")
    public AjaxResult disposalstatisticsInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(warnPreLogService.getById(id));
    }

    @GetMapping(value = "/warningstatisticsInfo/{id}")
    public AjaxResult warningstatisticsInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(warnHospitalLogService.getById(id));
    }

}
